﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;

namespace YJKExcelFormTool.Utilities
{
	public class AngleComparer : IComparer<MapEdge>
	{
		public AngleComparer(XYZ ptCurNode, double referenceStartAngle)
		{
			this.m_ptCurNode = ptCurNode;
			this.m_ReferenceStartAngle = referenceStartAngle;
		}

		public int Compare(MapEdge lhv, MapEdge rhv)
		{
			double num = 0.0;
			double num2 = 0.0;
			lhv.GetAngle(this.m_ptCurNode, ref num);
			num = Geometry.BetweenTheAngles(this.m_ReferenceStartAngle, num, true);
			rhv.GetAngle(this.m_ptCurNode, ref num2);
			num2 = Geometry.BetweenTheAngles(this.m_ReferenceStartAngle, num2, true);
			if (Geometry.IsEqual(num, num2))
			{
				int num3 = 0;
				int num4 = 0;
				lhv.GetWinding(this.m_ptCurNode, ref num3);
				rhv.GetWinding(this.m_ptCurNode, ref num4);
				if (num3 == num4)
				{
					if (num3 == -1)
					{
						if (lhv.Radius > rhv.Radius)
						{
							return -1;
						}
					}
					else if (lhv.Radius < rhv.Radius)
					{
						return -1;
					}
				}
				else if (num3 < num4)
				{
					return -1;
				}
			}
			else if (num < num2)
			{
				return -1;
			}
			return 1;
		}

		private XYZ m_ptCurNode;

		private double m_ReferenceStartAngle;
	}
}
